package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.edu.koTA07.XmlDataSet.BarangXmlDataSet;
import com.edu.koTA07.bean.BarangBean;
import com.edu.koTA07.sqlHelper.SQLHelper;
import com.edu.koTA07.takingOrder.R;

public class BarangUtils {
	private static final String TAG = "cek";
	SQLHelper helper;
	SQLiteDatabase database;	
	
	public BarangUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
	
	/* public ArrayList<String> getItemDetails(String orderId, String itemId){
	        Cursor c = db. query(this.ITEM_TABLE_NAME, new String[]      {this.ITEM_KEY_ITEM_ID_INC,this.ITEM_KEY_NAME,this.ITEM_KEY_BRAND,     this.ITEM_KEY_CURRENTQTY,this.ITEM_KEY_QTY,this.ITEM_KEY_PRICE,this.ITEM_KEY_DESCRIPTION},     this.ITEM_KEY_ID+" =? and "+ this.ITEM_KEY_ORDER_ID +" =?", new String[]{itemId,     orderId},null,null,null);
	    //TODO
	    c.moveToFirst();

	    ArrayList<String> data = new ArrayList<String>();
	    data.add(c.getString(c.getColumnIndex(this.ITEM_KEY_NAME)));
	    data.add(c.getString(c.getColumnIndex(this.ITEM_KEY_BRAND)));
	    data.add(((Double)     c.getDouble(c.getColumnIndex(this.ITEM_KEY_DESCRIPTION))).toString());
	    data.add(c.getString(c.getColumnIndex(this.ITEM_KEY_CURRENTQTY)));
	    data.add(c.getString(c.getColumnIndex(this.ITEM_KEY_QTY)));

	    return data;
	*/
	@SuppressWarnings("static-access")
	public BarangBean getBarangById(String IDBarang){
		BarangBean barang = new BarangBean();
		Cursor dataCursor = database. query(helper.TABLE_BARANG, new String[] {helper.ID_BARANG, helper.ID_KATEGORI_BARANG1, helper.NAMA_BARANG, 
				helper.HARGA_BARANG},     helper.ID_BARANG+" =?", new String[]{IDBarang},null,null,null);
		dataCursor.moveToFirst();
		String idBarang = dataCursor.getString(0);
		String idKategoriBarang = dataCursor.getString(1); 
		String namaBarang = dataCursor.getString(2);
		long hargaBarang = dataCursor.getLong(3); 
		Log.d(TAG, idBarang);
		Log.d(TAG, idKategoriBarang);
		Log.d(TAG, namaBarang);
		Log.d(TAG, hargaBarang+"");
						
		barang.setIDBarang(idBarang);
		barang.setIDKategori(idKategoriBarang);
		barang.setNamaBarang(namaBarang); 
		barang.setHargaBarang(hargaBarang); 
		dataCursor.close();
		return barang;
		
	}
	
	@SuppressWarnings("static-access")
	public ArrayList<BarangBean> loadData() {
		ArrayList<BarangBean> currentData = new ArrayList<BarangBean>();
		Cursor dataCursor = database.query(helper.TABLE_BARANG, 
				new String[] {helper.ID_BARANG, helper.ID_KATEGORI_BARANG1, helper.NAMA_BARANG, 
				helper.HARGA_BARANG}, 
				null, null, null, null, String.format("%s", helper.ID_BARANG));
		
		dataCursor.moveToFirst(); BarangBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				String idBarang = dataCursor.getString(0);
				String idKategoriBarang = dataCursor.getString(1); 
				String namaBarang = dataCursor.getString(2);
				long hargaBarang = dataCursor.getLong(3); 
								
				t = new BarangBean(); 
				t.setIDBarang(idBarang);
				t.setIDKategori(idKategoriBarang);
				t.setNamaBarang(namaBarang); 
				t.setHargaBarang(hargaBarang); 
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}

	
	@SuppressWarnings("static-access")
	public ArrayList<BarangBean> loadData(String idKategori) {
		ArrayList<BarangBean> currentData = new ArrayList<BarangBean>();
		Cursor dataCursor = database.query(helper.TABLE_BARANG, 
				new String[] {helper.ID_BARANG, helper.ID_KATEGORI_BARANG1, helper.NAMA_BARANG, 
				helper.HARGA_BARANG}, 
				helper.ID_KATEGORI_BARANG1+" =?", new String[]{idKategori}, null, null, String.format("%s", helper.ID_BARANG));
		dataCursor.moveToFirst(); BarangBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				String idBarang = dataCursor.getString(0);
				String idKategoriBarang = dataCursor.getString(1); 
				String namaBarang = dataCursor.getString(2);
				long hargaBarang = dataCursor.getLong(3); 
								
				t = new BarangBean(); 
				t.setIDBarang(idBarang);
				t.setIDKategori(idKategoriBarang);
				t.setNamaBarang(namaBarang); 
				t.setHargaBarang(hargaBarang); 
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}
	
	@SuppressWarnings("static-access")
	public void onSaveDataFromXml(BarangXmlDataSet barang) {
    	//assert (null != customer);
    	//helper.onCreate(database);
		
		for (int i = 0; i < barang.getNamaBarang().size(); i++) {
			ContentValues values = new ContentValues();
			values.put(helper.ID_BARANG, barang.getIDBarang().get(i));
			values.put(helper.ID_KATEGORI_BARANG1, barang.getIDKategori().get(i));
			values.put(helper.NAMA_BARANG, barang.getNamaBarang().get(i));
			Log.d(TAG, barang.getNamaBarang().get(i));
			Log.d(TAG, values.toString());
			
			values.put(helper.HARGA_BARANG, Long.parseLong(barang.getHargaBarang().get(i)));
			database.insert(helper.TABLE_BARANG, null, values);
    	}
		
    }
	
	AlertDialog alert;
    public void onShowData(BarangBean brng, Context ctx) {
		final BarangBean thisItem = brng;		
		alert = new AlertDialog.Builder(ctx).setIcon(R.drawable.icon)
	        .setTitle("Display Data")
	        .setMessage(" ------------ Barang -------------\n"
	        		  + "ID: "+thisItem.getIDBarang()+"\n" 
	        		  + "Nama Barang: "+thisItem.getNamaBarang()+"\n"
	        		  + "Harga Barang: "+thisItem.getHargaBarang()+"\n")
	        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	               alert.cancel();
	            }
	        }).create();			
		alert.show();
	}

}
